package white440.controller;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

import white440.common.RemoteController;
import white440.common.RemoteTruck;

public class RmiTestingController extends UnicastRemoteObject implements
RemoteController {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1848154313065350182L;
	private RemoteTruck truck;

	protected RmiTestingController() throws RemoteException {
		super();
		try{
			//			String truckLocation = null;
			//			try {
			//				truckLocation = "rmi://" 
			//					+ InetAddress.getLocalHost().getHostAddress() 
			//					+ "/" + RemoteTruck.RMINAME;
			//			} catch (UnknownHostException e1) {
			//				// TODO Auto-generated catch block
			//				e1.printStackTrace();
			//			}
			//			System.out.println("Truck at: " + truckLocation);

			//rmi
			Registry reg = LocateRegistry.getRegistry();
			reg.rebind(RemoteController.RMINAME, (RemoteController) this);

			//			try {
			//				Object obj = Naming.lookup(truckLocation);
			//				if(obj instanceof RemoteTruck){
			//					truck = (RemoteTruck)obj;
			//				}else{
			//					System.out.println("Err: not a truck");
			//				}
			//			} catch (MalformedURLException e) {
			//				// TODO Auto-generated catch block
			//				e.printStackTrace();
			//			} catch (NotBoundException e) {
			//				// TODO Auto-generated catch block
			//				e.printStackTrace();
			//			} 
			//
			//
			//			truck.setController(this);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public InetAddress getIpAddress() throws RemoteException {
		// TODO Auto-generated method stub
		InetAddress ans = null;
		try {
			ans = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ans;
	}

	@Override
	public boolean isConnected() throws RemoteException {
		return true;
	}

	private void doStuff() {
		final JFrame frame = new JFrame("Rmi Testing Controller");
		frame.setLayout(new GridLayout(2,2));

		//do stuff with the servos
		JButton butt = new JButton("move Servo");
		butt.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {		
				try {
					double rand = Math.random();
					System.out.println("Rand: " + rand);
					System.out.println("Servos set: " 
							+ (truck.setDireciton((int)(rand*180-90)) 
									&& truck.setThrottle((int)(rand*100))) );
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}				
		});
		frame.add(butt);

		//conectt to truck
		final JTextField ip = new JTextField();
		frame.add(ip);
		butt = new JButton("Find Truck");
		butt.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String loc = "rmi://" + ip.getText() + "/" + RemoteTruck.RMINAME;
				System.out.println("Connecting to truck at: " + loc);
				try {
					truck = (RemoteTruck) Naming.lookup(loc);
					setTruckController();
					//frame.add(new JLabel(truck.getCameraImage(0)));
					frame.add(new WebcamComponent(0, truck));
					frame.pack();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NotBoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					System.out.println("Truck is conneced: " + 
							truck.isConnected());
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}


		});
		frame.add(butt);
		
		

		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

	}

	private void setTruckController() throws RemoteException {
		truck.setController(this);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			RmiTestingController test = new RmiTestingController();
			test.doStuff();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}



}
